#include "Shot.h"
#include "Ship.h"
#include "Enemy.h"
#include "Level.h"

Shot::Shot(Ship* p_ownerPam, int directionPam)
{
    p_owner = p_ownerPam;
    direction = directionPam;
    Speed = CVector(300,300);
    Create("../../resource/shot_player.bmp",6,22);
}

Shot::~Shot()
{

}

void Shot::Execute()
{
    HandleCollision();
    HandleLimit();
    HandleFire();
}

void Shot::HandleLimit()
{
    if(Position.fy < 0 || Position.fy > CVideoManager::getInstance()->WindowSize.fy){
        bVisible = false;
        bCanDelete = true;
    }
}

void Shot::HandleFire()
{
    if(direction == 1)
        Position.fy -= Speed.fy * CTimerManager::getInstance()->fFrameTime;
    else
        Position.fy += Speed.fy * CTimerManager::getInstance()->fFrameTime;
}

void Shot::HandleCollision()
{
    std::vector<CSprite*>::const_iterator itr;
    for(itr=CCore::getInstance()->GetCurrentScene<Level>()->layerScreen->Sprites.begin();
        itr!=CCore::getInstance()->GetCurrentScene<Level>()->layerScreen->Sprites.end();itr++)
    {
        if(dynamic_cast<Ship*>(*itr)){
            if(GetFrame().Collide((*itr)->GetFrame()) && (*itr)->bVisible &&
                p_owner != dynamic_cast<Ship*>(*itr) && bVisible){
                dynamic_cast<Ship*>(*itr)->lifePoints-=1;
                bVisible = false;
                bCanDelete = true;
            }
        }
    }
}
